Step 3: ES6 Module
Let’s update the package.json
file:
{
"name": "hello-node",
"version": "1.0.0",
"main": "server.js",
"license": "MIT",
"scripts": {
"start": "node server.js"
}
}
You should now be able to run the app through the following command in the terminal:
yarn start
Notice the import statements in the server.js
file.
const fs = require("fs"); // import File System module
const http = require("http"); // import HTTP Networking module
Nodes default module system is based on CommonJS. In order to use ES6 module system with Node, add “type”: “module”
to package.json
:
{
"name": "hello-node",
"version": "1.0.0",
"main": "server.js",
"license": "MIT",
"type": "module",
"scripts": {
"start": "node server.js"
}
}
Now update the server.js
as follows:
import fs from "fs"; // import File System module
import http from "http"; // import HTTP Networking module
console.log("Hello Node!");
// console.log(window); // not available in Node
// console.log(document); // not available in Node
console.log(fs);
console.log(http);
Run the Node app by yarn start
.